<template>
  <div class="discountList">
    <!-- 头部导航 -->
    <header>
      <div  
        @click="selectType(index)"
        :class="{navBar_active:typeIndex==index}"
        v-for="(type,index) of typeList" 
        :key="index">
        {{type}}
      </div>
    </header>
    <!-- 头部导航结束 -->
    <!-- 列表 -->
    <div class="itemList" v-if='discountData.length!=0'>
      <div class="item" v-for="(item,index) of discountData" :key="index" @click="toMydiscount(item)">
        <div class="item_left">
          <img :src="item.goods_info.thumb_url" alt="" mode="aspectFill" lazy-load="true">
        </div>
        <div class="item_right">
          <div class="item_right_tit">
            {{item.goods_info.name}}
          </div>
          <div class="item_right_bar">
            <van-progress :percentage="item.percentage" color="#ff7b00"  :pivot-text="item.bargain_text"/>
            <!-- <van-progress  :percentage="item.percentage" pivot-text="橙色"/> -->
            <div class="item_right_bar_desc">已砍价至￥{{item.finally_money}}</div>
          </div>
          <div class="item_right_price">
            <span>最低价</span><span>{{item.lowest_price}}</span>
          </div>
        </div>
      </div>
    </div>
    <noContent v-else></noContent>
    <!-- 列表结束 -->
  </div>
</template>
<script>
import noContent from '@/components/noContent';
export default {
  name:'discountList',
  data() {
    return {
      typeIndex:0,
      typeList:['全部','进行中','已结束'],
      discountData:[],
      page:0
    }
  },
  components:{
    noContent
  },
  methods: {
    selectType(index){
      this.typeIndex=index
      this.page=0;
      this.discountData=[]
      switch(index){
        case 0:this.getDiscountList();break;
        case 1:this.getDiscountList('processing');break;
        case 2: this.getDiscountList('over');break;
      }
    },
    async getDiscountList(type){
        wx.showLoading()
        const userToken=wx.getStorageSync('userToken')
        const res=await this.$request.getData('/activity/getUserInitiateBargain',{
          user_token:userToken,
          type,
          page:this.page
        })
        // res.data.level=='success'
        if(res.data.level=='success'){
          const data=res.data.data
          data.forEach( (item,index,arr)=>{
            const percentage=item.bargain_money*100/item.goods_info.s_price
            console.log(item.bargain_money,item.goods_info.s_price)
            arr[index].percentage=percentage
            arr[index].bargain_text=`￥${item.finally_money}`
            
          })
          this.discountData=[...this.discountData,...res.data.data]
        }
        wx.hideLoading()
      },
      toMydiscount(item){
        this.$currency.navigateTo(`/pages/discount/discountDetail/main?id=${item.id}`)
      }
  },
  onShow(){
    this.page=0;
    this.discountData=[]
    this.getDiscountList()
  },
  onReachBottom(){
    this.page++;
    this.getDiscountList()
  }
}
</script>
<style lang="scss" scoped>
  .discountList{
    width:100%;
    background: #eee;
    min-height:100%;

  }
  // 头部导航
  header{
    background: #fff;
    display:flex;
    text-align: center;
    align-items:space-around;
    div{
      flex:1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
  }
  .navBar_active{
    color:red;
  }
  .navBar_active::after{
    content:'';
    height: 2px;
    margin-top:5px;
    width:60px;
    background: red;
  }

  // 列表项
  .itemList{
    margin-top:20px;
    padding:0 10px;
    background: #fff;
    .item{
      display: flex;
      padding:10px;
      justify-content: space-around;
      align-items: center;
      img{
        width: 100px;
        height: 100px;
      }
      .item_right{
        width:200px;
        &_tit{
          width: 100%;
          overflow:hidden; 
          text-overflow:ellipsis;
          display:-webkit-box; 
          -webkit-box-orient:vertical;
          -webkit-line-clamp:2;
          margin-bottom: 10px;
        }
        &_bar{
          margin-bottom: 15px;
          &_desc{
            margin-top:20px;
          }
        }
        &_price{
          & span:last-child{
            color:#ff7b00;
            font-size: 18px;
          }
        }
      }
    }
  }

</style>
<style>
  /* .discountList .van-progress{
    height: 6px;
  } */
</style>

